/**
 * @copyright 2017 sinping.com 北京芯平科技股份有限公司. All rights reserved.
 * 本内容仅限于北京芯平科技股份有限公司内部传阅，禁止外泄以及用于其他的商业目的.
 */

package com.sinping.qh.api.support;

import com.baomidou.mybatisplus.plugins.Page;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.sinping.qh.utils.time.DateFormatUtil;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 页面传递的分页参数
 *
 * @author 刘庆沛
 * @create 2017/12/21
 */
@Data
public class FrontPage<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 总条数
     */
    private int total;
    /**
     * 每页大小，默认10
     */
    @JsonProperty(value = "page_size")
    private int size;
    /**
     * 总页数
     */
    @JsonProperty(value = "page_total")
    private int pages;
    /**
     * 当前页数
     */
    @JsonProperty(value = "page_current")
    private int page;
    /**
     * 需要排序的列
     */
    @JsonProperty(value = "order_column")
    private String prop;
    /**
     * 排序方式 desc asc
     */
    @JsonProperty(value = "order_by")
    private String order;
    /**
     * 消息记录
     */
    @JsonProperty(value = "items")
    private List<T> records;
    /**
     * 其他查询条件
     */
    private Map<String, Object> condition;

    @JsonIgnore
    private Map<String, String> statistics;

    /***由于一些参数再condition Map对象里，
     * 使用时取不方便如果参数值不为空
     * 则设置该参数名对应的值 方便用page对象直接获取
     */
    @JsonIgnore
    private String name = null;
    @JsonIgnore
    private String email = null;
    @JsonIgnore
    private String identityNo = null;
    @JsonIgnore
    private Date startDate = null;
    @JsonIgnore
    private Date endDate = null;
    @JsonIgnore
    private Integer state = null;
    @JsonIgnore
    private String mobile = null;
    @JsonIgnore
    private String fullName = null;

    @JsonIgnore
    private String checkPlanId = null;

    @JsonIgnore
    private String checkStatus = null;

    @JsonIgnore
    private String checkRecordId = null;

    @JsonIgnore
    private String hiddenDangerStatus = null;

    @JsonIgnore
    private String checkUnitId = null;

    @JsonIgnore
    private String checkBuildingId = null;

    @JsonIgnore
    private String hiddenDangerId = null;
    @JsonIgnore
    private String unitType = null;
    @JsonIgnore
    private String unitName = null;


    public void addStatistics(String key, String value) {
        if (this.statistics == null) {
            this.statistics = new HashMap<>();
        }
        statistics.put(key, value);
    }

    public Map<String, Object> getCondition() {
        return condition;
    }

    public void setCondition(Map<String, Object> condition) throws Exception {

        this.condition = condition;
        if (null != condition && !condition.isEmpty()) {
            if (condition.containsKey("name") && !"".equals(condition.get("name").toString())) {
                name = condition.get("name").toString();
            }
            if (condition.containsKey("email") && !"".equals(condition.get("email").toString())) {
                email = condition.get("email").toString();
            }
            if (condition.containsKey("identity_no") && !"".equals(condition.get("identity_no").toString())) {
                identityNo = condition.get("identity_no").toString();
            }
            if (condition.containsKey("start_time") && !"".equals(condition.get("start_time").toString())) {
                startDate = DateFormatUtil.pareDate(DateFormatUtil.PATTERN_DEFAULT_ON_SECOND, condition.get("start_time").toString());
            }
            if (condition.containsKey("end_time") && !"".equals(condition.get("end_time").toString())) {
                endDate = DateFormatUtil.pareDate(DateFormatUtil.PATTERN_DEFAULT_ON_SECOND, condition.get("end_time").toString());
            }
            if (condition.containsKey("state") && !"".equals(condition.get("state").toString())) {
                state = Integer.valueOf(condition.get("state").toString());
            }
            if (condition.containsKey("mobile") && !"".equals(condition.get("mobile").toString())) {
                mobile = condition.get("mobile").toString();
            }
            if (condition.containsKey("full_name") && !"".equals(condition.get("full_name").toString())) {
                fullName = condition.get("full_name").toString();
            }
            if (condition.containsKey("checkplan_id") && !"".equals(condition.get("checkplan_id").toString())) {
                checkPlanId = condition.get("checkplan_id").toString();
            }
            if (condition.containsKey("check_status") && !"".equals(condition.get("check_status").toString())) {
                checkStatus = condition.get("check_status").toString();
            }
            if (condition.containsKey("checkrecord_id") && !"".equals(condition.get("checkrecord_id").toString())) {
                checkRecordId = condition.get("checkrecord_id").toString();
            }
            if (condition.containsKey("hidden_danger_status") && !"".equals(condition.get("hidden_danger_status").toString())) {
                hiddenDangerStatus = condition.get("hidden_danger_status").toString();
            }
            if (condition.containsKey("check_unit_id") && !"".equals(condition.get("check_unit_id").toString())) {
                checkUnitId = condition.get("check_unit_id").toString();
            }
            if (condition.containsKey("check_building_id") && !"".equals(condition.get("check_building_id").toString())) {
                checkBuildingId = condition.get("check_building_id").toString();
            }
            if (condition.containsKey("hidden_danger_id") && !"".equals(condition.get("hidden_danger_id").toString())) {
                hiddenDangerId = condition.get("hidden_danger_id").toString();
            }
            if (condition.containsKey("unit_type") && !"".equals(condition.get("unit_type").toString())) {
                unitType = condition.get("unit_type").toString();
            }
            if (condition.containsKey("unit_name") && !"".equals(condition.get("unit_name").toString())) {
                unitName = condition.get("unit_name").toString();
            }
            for (String key : condition.keySet()) {
                if ("".equals(condition.get(key).toString())){
                    condition.put(key, null);
                }


            }


        }

    }


    @Override
    public String toString() {
        return "FrontPage{" +
                "total=" + total +
                ", size=" + size +
                ", pages=" + pages +
                ", page=" + page +
                ", prop='" + prop + '\'' +
                ", order='" + order + '\'' +
                ", records=" + records +
                ", condition=" + condition +
                '}';
    }

    public FrontPage() {
        this.page = 1;
        this.size = 10;
    }

    /**
     * mp_page转为前台需要的page
     *
     * @param page
     */
    public FrontPage(Page<T> page) {
        this.size = page.getSize();
        this.page = page.getCurrent();
        this.pages = page.getPages();
        this.total = page.getTotal();
        if (page.isAsc()) {
            this.order = "asc";
        } else {
            this.order = "desc";
        }
        this.prop = page.getOrderByField();
        this.records = page.getRecords();
    }

    //前台传的page转为mp_plus page
    public Page<T> toPagePlus() {
        Page<T> pagePlus = new Page<T>();

        if (this.page <= 1) {
            pagePlus.setCurrent(1);
        } else {
            pagePlus.setCurrent(this.page);
        }

        if (this.size <= 0) {
            pagePlus.setSize(10);
        } else {
            pagePlus.setSize(this.size);
        }

        if (StringUtils.isBlank(this.prop)) {
            pagePlus.setAsc(false);
            pagePlus.setOrderByField("create_time");
        } else {
            pagePlus.setAsc("asc".equals(this.order));
            pagePlus.setOrderByField(this.prop);
        }

        return pagePlus;
    }


}
